<template>
  <div class="companys-manage">
    <div class="companys-app">
      <el-card class="box-card">
        <!-- 头部搜索 -->
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <div class="head-search">
            <el-row :gutter="20">
              <el-col :span="5">
                <el-form-item label="标签名称:">
                  <el-input
                    v-model="formInline.tags"
                    placeholder="请选择"
                  ></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="市:" prop="province">
                  <el-select
                    @change="getProvince"
                    v-model="formInline.province"
                    filterable
                  >
                    <el-option
                      v-for="item in citySelect.province"
                      :key="item"
                      :label="item"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="区:" prop="city">
                  <el-select
                    v-model="formInline.city"
                    filterable
                    class="filter-item"
                    style="width: 120px;"
                  >
                    <el-option
                      :key="item"
                      v-for="item in citySelect.cityDate"
                      :label="item"
                      :value="item"
                    ></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="5">
                <el-form-item label="企业简称:">
                  <el-input v-model="formInline.shortName"></el-input>
                </el-form-item>
              </el-col>
              <el-col :span="3">
                <el-form-item label="状态:" prop="state">
                  <el-select v-model="formInline.state" placeholder="请输入">
                    <el-option label="启用" value="1"></el-option>
                    <el-option label="禁用" value="0"></el-option>
                  </el-select>
                </el-form-item>
              </el-col>
              <el-col :span="2">
                <el-form-item label="操作:">
                  <el-button
                    type="primary"
                    placeholder="请输入"
                    icon="el-icon-search"
                    size="small"
                    @click="getLists"
                    >搜索</el-button
                  >
                </el-form-item>
              </el-col>
            </el-row>
            <el-button
              class="filter-item fr"
              type="primary"
              icon="el-icon-edit"
              size="small"
              style="margin-left:10px
            "
              @click="dialogAdduser = true"
              >新增用户</el-button
            >
          </div>
        </el-form>
        <el-alert
          :title="alertText"
          type="info"
          :closable="false"
          show-icon
          class="datas"
        >
        </el-alert>
        <!-- 表格数据 -->
        <el-table :data="tableData" border style="width: 100%">
          <el-table-column prop="id" label="序号"> </el-table-column>
          <el-table-column prop="number" label="企业编号"> </el-table-column>
          <el-table-column prop="company" label="企业简称"> </el-table-column>
          <el-table-column prop="tags" label="标签"> </el-table-column>
          <el-table-column prop="creatorID" label="创建者"> </el-table-column>
          <el-table-column prop="addDate" label="创建日期"> </el-table-column>
          <el-table-column prop="remarks" label="备注"> </el-table-column>
          <el-table-column prop="state" label="状态">
            <template slot-scope="scope">
              <span v-if="scope.row.state == 1">启用</span>
              <span v-else>禁用</span>
            </template>
          </el-table-column>
          <el-table-column
            prop="caozuo"
            label="操作"
            width="280%"
            align="center"
          >
            <template slot-scope="scope">
              <el-button
                type="primary"
                size="small"
                @click="dialogAdduser = true"
                >编辑</el-button
              >
              <el-button
                type="primary"
                size="small"
                @click="upStatus(scope.row)"
              >
                <span v-if="scope.row.state == 0">启用</span>
                <span v-else>禁用</span></el-button
              >
              <el-button
                type="danger"
                size="small"
                @click="removeData(scope.row.id)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="formInline.page"
          :page-sizes="[10, 20, 30, 50]"
          :page-size="formInline.pagesize"
          layout=" sizes, prev, pager, next, jumper"
          :total="total"
          background
        >
        </el-pagination>
      </el-card>
    </div>
    <!-- 新增用户的对话框 -->
    <div class="add-from">
      <el-dialog title="创建用户" :visible.sync="dialogAdduser" width="50%">
        <el-form
          ref="dataForm"
          :label-position="labelPosition"
          :rules="ruleInline"
          :model="formData"
          label-width="150px"
          style="width: 80%; margin-left:10px;"
        >
          <el-form-item label="企业名称" prop="shortName">
            <el-input v-model="formData.shortName"></el-input>
            <el-checkbox v-model="formData.isFamous">是否为名企</el-checkbox>
          </el-form-item>
          <el-form-item label="所属公司" prop="company">
            <el-input v-model="formData.company"></el-input>
            <p>https://www.tianyancha.com （在此可查询所属公司全称及简称）</p>
          </el-form-item>
          <el-form-item label="市" prop="province">
            <el-select
              v-model="formData.province"
              placeholder="请选择"
              filterable
              class="filter-item"
              style="width: 130px;"
            >
              <el-option
                v-for="item in citySelect.province"
                :key="item"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
            <!-- 区 -->
            <el-select
              v-model="formData.city"
              placeholder="请选择"
              class="filter-item"
              style="width: 130px;"
              filterable
            >
              <el-option
                :key="item"
                v-for="item in citySelect.cityDate"
                :label="item"
                :value="item"
              >
              </el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="方向（企业标签）" prop="tags">
            <el-input v-model="formData.tags"></el-input>
          </el-form-item>
          <el-form-item label="备注" prop="remarks">
            <el-input
              type="textarea"
              :autosize="{ minRows: 2, maxRows: 4 }"
              placeholder="请输入"
              v-model="formData.remarks"
            ></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="dialogAdduser = false">取 消</el-button>
          <el-button type="primary" @click="addUsers">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  getLists,
  removeById,
  isDisable,
  status,
  addData
} from '@/api/company.js'
import { citys, provinces } from '@/api/citys.js'
export default {
  name: 'companys',
  components: {},
  props: {},
  data() {
    return {
      formInline: {
        page: 1,
        pagesize: 10
      },
      // 总共的数据数
      alertText: '',
      // 接收表格数据
      tableData: [],
      // 总数据条数
      total: 0,
      // 表格名称数据
      formData: {
        isFamous: '',
        shortName: '',
        company: '',
        province: '',
        city: '',
        tags: '',
        remarks: ''
      },
      // 新增用户的弹框
      dialogAdduser: false,
      labelPosition: 'right',
      formBase: {
        type: Object,
        required: true
      },
      value: '',
      // 市区数据请求数据
      citySelect: {
        province: [],
        cityDate: []
      },
      // 表单验证
      ruleInline: {
        shortName: [
          { required: true, message: '企业简称不能为空', trigger: 'blur' }
        ],
        province: [
          { required: true, message: '请选择省份', trigger: 'change' }
        ],
        tags: [{ required: true, message: '请请输标签', trigger: 'blur' }]
      }
    }
  },
  computed: {
    status() {
      return status
    }
  },
  watch: {},
  created() {
    this.getLists()
    // 获取到城市
    this.getCityData()
    // 获取到区
    this.getProvince()
  },
  mounted() {},
  methods: {
    // 获取列表数据
    async getLists() {
      try {
        const { data } = await getLists(this.formInline)
        this.tableData = data.items
        // 获取总数据条数
        this.total = data.counts
        this.alertText = `共${this.total}条数据`

        // console.log(data)
        this.$message.success('获取数据成功')
      } catch (err) {
        this.$message.error('获取数据失败')
      }
    },
    // 搜索
    handleSizeChange(newSize) {
      // 当页号发生改变时,更改pagesize,重新请求
      this.formInline.pagesize = newSize
      this.getLists()
    },
    handleCurrentChange(newPage) {
      // 当页码发生改变时,更改pagesize,重新请求
      this.formInline.page = newPage
      this.getLists()
    },
    // 新增用户
    async addUsers() {
      try {
        const { data } = await addData(this.formData)
        console.log(data, 1111111111111111)
        this.$message.success('添加用户成功')
      } catch (err) {
        this.$message.error('获取用户失败')
      }
      // 关闭弹框
      this.dialogAdduser = false
      // 重新获取列表数据
      this.getLists()
    },
    // 获取到城市
    getCityData: function() {
      this.citySelect.province = provinces()
    },
    // 选省获取到区
    getProvince: function(e) {
      this.citySelect.cityDate = citys(e)
      this.formInline.city = this.citySelect.cityDate[0]
    },
    // 启用、禁用
    upStatus(val) {
      var status = ''
      if (val.state === true) {
        val.state = 0
        status = '禁用'
      } else {
        val.state = 1
        status = '启用'
      }
      var data = {
        id: val.id,
        state: val.state
      }
      this.$confirm('已成功' + status + ', 是否继续?', '提示', {
        type: 'warning'
      })
        .then(async () => {
          await isDisable(data)
            .then(response => {
              this.$message.success('已成功' + status + '!')
              this.getLists()
            })
            .catch(response => {
              this.$message.error(status + '失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    },

    // 删除数据
    async removeData(user) {
      this.$confirm('将删除该用户, 是否确定?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          await removeById({ id: user })
            .then(response => {
              this.$message.success('成功删除了用户' + '!')
              this.tableData.splice(user, 1)
              this.getLists()
            })
            .catch(response => {
              this.$message.error('删除失败!')
            })
        })
        .catch(() => {
          this.$message.info('已取消操作!')
        })
    }
  }
}
</script>

<style scoped lang="less">
.companys-manage {
  padding: 10px 5px 0 5px;
}
.filter-item {
  display: inline-block;
  vertical-align: middle;
  margin-bottom: 10px;
}
.fr {
  float: right;
}
.datas {
  margin-bottom: 10px;
}
/deep/.el-form-item__label {
  font-weight: bold;
}
</style>
